<?

if ($c = stack('name')) {

	$_id_component	= $DATA['components'][$c]['id'];			//	list id value
	$_id_data		= D_MB_DATA;			//	meta id value
	$_id_meta		= D_MB_META;			//	meta id value

	$_lang = context('lang');

	$_table = 'data, data meta';
	
	$_crit = "data.id_data=$_id_data AND data.id_parent=$_id_component AND ".dbAndL('data.attr', AA_VISIBLE)." AND meta.id_data=$_id_meta AND meta.id_parent=data.id AND meta.name='title' AND meta.assoc='$_lang' AND meta.value IS NOT NULL";

	$s = array_extend( array('path' => 'article', 'w' => 128, 'h' => 96, 'param' => 'id', 'caption' => '', 'perPage' => 5, 'showBody' => '', 'showDate' => ''), $DATA['components'][$c]['settings']);

	$s['path'] = createdir(FP_UPLOAD_PATH.$s['path'].'/');
	$s['h'] = (int)$s['h'];
	$s['w'] = (int)$s['w'];


//dbdebug();
	
	$num = datafetch('db_item', tb($_table), $_crit, "COUNT(*) as num");

//debug($num, 'num');


	if ($num = $num['num']) {
		$DATA['components'][$c]['settings']['paging'] = createPaging(array(
			'num' => $num, 
			'page' => param('page'), 
			'perPage' => $s['perPage']
		));
	
		$DATA['components'][$c]['data'] = datafetch_array('db_list', array(
			'select' => 'data.*',
			'table' => tb($_table), 
			'where' => $_crit,	
			'order' => 'data.id_index', 
			'index' => 'id', 
			'limit' => $DATA['components'][$c]['settings']['paging']['limit'] 
		));

		$DATA['components'][$c]['meta'] = datafetch_array('loadmeta', $_id_meta, 
			array('parent' => array_keys($DATA['components'][$c]['data']), 'assoc' => $_lang, 'getclob' => true, 'source' => true));

		
		foreach($DATA['components'][$c]['meta'] as $k => $v) {
			if ($v['assoc'])
				$DATA['components'][$c]['data'][$v['id_parent']]['meta'][$v['name']][$v['assoc']] = $v['value'];
			else
				$DATA['components'][$c]['data'][$v['id_parent']][$v['name']] = $v['value'];
		}
	
		foreach($DATA['components'][$c]['data'] as $k => $v) {

			if (isset($v['meta'])) 	// parse language dependent data
				foreach($v['meta'] as $mname => $mv) {
					if ($mv[$_lang] && ($mv[$_lang] != -1))
						$DATA['components'][$c]['data'][$k][$mname] = $mv[$_lang];
					else {	// no data in current language, get any other we can find
						foreach($DATA['langs'] as $lid => $lv) {
							if ($mv[$lid] && ($mv[$lid] != -1)) {
								$DATA['components'][$c]['data'][$k][$mname] = $mv[$lid];
								break;
							}
						}
					}
				}
				
//			$DATA['components'][$c]['data'][$k] = array_merge($v, datafetch_array('meta', $_id_meta, $k, $CONTEXT['lang']));
			$DATA['components'][$c]['data'][$k]['image'] = checkImage($s['path'], $k);
			$DATA['components'][$c]['data'][$k]['date'] = format_simple_date($v['date_create']);
		}
	
	} else 
		$DATA['components'][$c]['data'] = array();

	unstack();
}
